package com.ats.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ats.dao.TeacherDao_xh;
import com.ats.model.Student;
import com.ats.model.Teacher;
import com.ats.model.User;
import com.ats.util.JDBCUtil;

import net.sf.json.JSONArray;
/**
 * 老师个人功能的DAO层实现类
 * @author xh
 *
 */
public class TeacherDaoImpl_xh implements TeacherDao_xh{

	@Override
	public String FindTnameByTid(int tid) {
		// TODO Auto-generated method stub
		String tname ="";
		StringBuffer  sql = new StringBuffer();
		sql.append(" select name from ats_teacher");
		sql.append(" where tid= ?");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst =null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,tid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				tname=rs.getString("name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tname;
	}

	@Override
	public JSONArray FindTeacherByUid(int uid) {
		// TODO Auto-generated method stub
		JSONArray jso = new JSONArray();
		StringBuffer  sql = new StringBuffer();
		sql.append(" select ats_teacher.tid,ats_teacher.`name`,ats_teacher.age,");
		sql.append(" ats_teacher.address,ats_teacher.number,ats_user.uid,");
		sql.append(" ats_user.uname,ats_user.upsw");
		sql.append(" from ats_teacher");
		sql.append(" inner join ats_user");
		sql.append(" on ats_teacher.tid = ats_user.tid");
		sql.append(" where ats_user.uid = ? ");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst =null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,uid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Teacher ta = new Teacher();
				ta.setTid(rs.getInt("tid"));
				ta.setName(rs.getString("name"));
				ta.setAge(rs.getInt("age"));
				ta.setAddress(rs.getString("address"));
				ta.setNumber(rs.getString("number"));
				ta.setUid(rs.getInt("uid"));
				ta.setUname(rs.getString("uname"));
				ta.setUpsw(rs.getString("upsw"));
				jso.add(ta);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jso;
	}

	@Override
	public boolean updateTeacherByTid(String name, int age, String address, String number, int tid) {
		// TODO Auto-generated method stub
		boolean flag =false;
		StringBuffer  sql = new StringBuffer();
		sql.append("update ats_teacher set ats_teacher.`name`=?,");
		sql.append("ats_teacher.age=?,ats_teacher.address=?,");
		sql.append("ats_teacher.number=?");
		sql.append(" where tid = ?");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst =null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, name);
			pst.setInt(2, age);
			pst.setString(3, address);
			pst.setString(4, number);
			pst.setInt(5, tid);
			int i =pst.executeUpdate();
			if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean updateTeacherPswByTid(String uname, String upsw, int tid) {
		// TODO Auto-generated method stub
		boolean flag =false;
		StringBuffer  sql = new StringBuffer();
		sql.append("update ats_user set uname=?,upsw=? ");
		sql.append(" where tid=?");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst =null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			pst.setString(2, upsw);
			pst.setInt(3, tid);
			int i =pst.executeUpdate();
			if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int findTidByUid(int uid) {
		// TODO Auto-generated method stub
		int tid=0;
		StringBuffer  sql = new StringBuffer();
		sql.append(" select tid from ats_user");
		sql.append(" where uid= ?");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst =null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,uid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				tid=rs.getInt("tid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tid;
	}




}
